// 示例电影数据（可替换为从API获取的真实数据）
const movieData = [
    {
        id: "0.5毫米",
        rating: 8.2,
        reviewCount: "9027",
        link: "https://movie.douban.com/subject/20141108/",
        details: "['安藤樱', '织本顺吉', '木内みどり', '土屋希望', '井上竜夫']"
    },
    {
        id: "0.5的爱情",
        rating: 4.3,
        reviewCount: "(评价人数不足)",
        link: "https://movie.douban.com/subject/26266893/",
        details: "['蒲巴甲', '江语晨', '李悦铭', '刘芸', '程前']"
    },
    {
        id: "007：大破量子危机",
        rating: 6.6,
        reviewCount: "234",
        link: "https://movie.douban.com/subject/2132490/",
        details: "['丹尼尔·克雷格', '欧嘉·柯瑞兰寇', '马修·阿马立克', '朱迪·丹奇', '吉安卡罗·吉安尼尼']"
    },
    {
        id: "007：大破天幕杀机",
        rating: 7.2,
        reviewCount: "567",
        link: "https://movie.douban.com/subject/6786002/",
        details: "['丹尼尔·克雷格', '哈维尔·巴登', '拉尔夫·费因斯', '娜奥米·哈里斯', '贝纳尼丝·玛尔洛']"
    }
];

// 填充表格数据
function populateTable(data) {
    const tableBody = document.querySelector('#movieTable tbody');
    tableBody.innerHTML = ''; // 清空现有内容
    
    data.forEach(movie => {
        const row = document.createElement('tr');
        
        // 标识列
        const idCell = document.createElement('td');
        idCell.textContent = movie.id;
        row.appendChild(idCell);
        
        // 评分列
        const ratingCell = document.createElement('td');
        ratingCell.className = "rating-cell";
        ratingCell.textContent = movie.rating;
        row.appendChild(ratingCell);
        
        // 评价人数列
        const reviewCountCell = document.createElement('td');
        const formattedCount = formatReviewCount(movie.reviewCount);
        if (formattedCount === "(评价人数不足)") {
            reviewCountCell.className = "review-count-low";
        } else if (parseInt(movie.reviewCount) > 5000) {
            reviewCountCell.className = "review-count-high";
        }
        reviewCountCell.textContent = formattedCount;
        row.appendChild(reviewCountCell);
        
        // 链接列
        const linkCell = document.createElement('td');
        const link = document.createElement('a');
        link.href = movie.link;
        link.textContent = "查看详情";
        link.target = "_blank";
        link.style.color = "#0066cc";
        linkCell.appendChild(link);
        row.appendChild(linkCell);
        
        // 详细信息列
        const detailsCell = document.createElement('td');
        detailsCell.className = "details-cell";
        detailsCell.textContent = movie.details;
        row.appendChild(detailsCell);
        
        tableBody.appendChild(row);
    });
}

// 搜索过滤功能
function filterTable() {
    const input = document.getElementById('searchInput');
    const filter = input.value.toLowerCase();
    const rows = document.querySelectorAll('#movieTable tbody tr');
    
    rows.forEach(row => {
        const id = row.cells[0].textContent.toLowerCase();
        row.style.display = id.includes(filter) ? "" : "none";
    });
}

// 按评分排序功能
function sortByRating() {
    const tableBody = document.querySelector('#movieTable tbody');
    const rows = Array.from(tableBody.querySelectorAll('tr'));
    
    // 按评分降序排列
    rows.sort((a, b) => {
        const ratingA = parseFloat(a.cells[1].textContent);
        const ratingB = parseFloat(b.cells[1].textContent);
        return ratingB - ratingA;
    });
    
    // 重新插入排序后的行
    rows.forEach(row => tableBody.appendChild(row));
}

// 格式化评价人数显示
function formatReviewCount(text) {
    if (text === "(评价人数不足)") {
        return text;
    }
    const number = parseInt(text);
    if (isNaN(number)) {
        return text;
    }
    if (number > 10000) {
        return `${(number / 10000).toFixed(1)}万`;
    }
    if (number > 1000) {
        return `${Math.round(number / 1000)}千`;
    }
    return number.toString();
}

// 初始化表格
populateTable(movieData);